package com.wandousoushu.jiusen.base

import android.content.Context
import android.os.Bundle
import android.util.AttributeSet
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.WindowManager
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import com.wandousoushu.jiusen.R
import com.wandousoushu.jiusen.booksource.constant.AppConst
import com.wandousoushu.jiusen.booksource.constant.Theme
import com.wandousoushu.jiusen.lib.theme.ATH
import com.wandousoushu.jiusen.lib.theme.backgroundColor
import com.wandousoushu.jiusen.lib.theme.primaryColor
import com.wandousoushu.jiusen.util.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel


abstract class BaseActivity(
        private val layoutID: Int,
        private val fullScreen: Boolean = true,
        private val theme: Theme = Theme.Auto,
        private val transparent: Boolean = false
) : AppCompatActivity(),
    CoroutineScope by MainScope() {

    override fun attachBaseContext(newBase: Context) {
        super.attachBaseContext(LanguageUtils.setConfiguration(newBase))
    }

    override fun onCreateView(
        parent: View?,
        name: String,
        context: Context,
        attrs: AttributeSet
    ): View? {
        if (AppConst.menuViewNames.contains(name) && parent?.parent is FrameLayout) {
            (parent.parent as View).setBackgroundColor(backgroundColor)
        }
        return super.onCreateView(parent, name, context, attrs)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        window.decorView.disableAutoFill()
        initTheme()
        setupSystemBar()
        super.onCreate(savedInstanceState)
        setContentView(layoutID)
        onActivityCreated(savedInstanceState)
        observeLiveBus()
    }

    override fun onDestroy() {
        super.onDestroy()
        cancel()
    }

    abstract fun onActivityCreated(savedInstanceState: Bundle?)

    final override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        return menu?.let {
            val bool = onCompatCreateOptionsMenu(it)
            it.applyTint(this, theme)
            bool
        } ?: super.onCreateOptionsMenu(menu)
    }

    open fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
        return super.onCreateOptionsMenu(menu)
    }

    open fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
        return super.onOptionsItemSelected(item)
    }

    private fun initTheme() {
        when (theme) {
            Theme.Transparent -> setTheme(R.style.AppTheme_Transparent)
            Theme.Dark -> {
                setTheme(R.style.AppTheme_Dark)
                ATH.applyBackgroundTint(window.decorView)
            }
            Theme.Light -> {
                setTheme(R.style.AppTheme_Light)
                ATH.applyBackgroundTint(window.decorView)
            }
            else -> {
                if (ColorUtils.isColorLight(primaryColor)) {
                    setTheme(R.style.AppTheme_Light)
                } else {
                    setTheme(R.style.AppTheme_Dark)
                }
                ATH.applyBackgroundTint(window.decorView)
            }
        }
    }

    private fun setupSystemBar() {
        if (fullScreen) {
            window.clearFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                        or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
            )
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        }
        ATH.setStatusBarColorAuto(this, fullScreen)
        if (theme == Theme.Dark) {
            ATH.setLightStatusBar(this, false)
        } else if (theme == Theme.Light) {
            ATH.setLightStatusBar(this, true)
        }
        upNavigationBarColor()
    }

    open fun upNavigationBarColor() {
        ATH.setNavigationBarColorAuto(this)
    }

    open fun observeLiveBus() {
    }

    override fun finish() {
        currentFocus?.hideSoftInput()
        super.finish()
    }
}